from src.tablet.predict.tablet_price_predict import TabletPredictor
from src.tablet.preprocessor.model_data_preprocessor import process_predict_data
from src.utils.config import logger
import time
tablet_predictor = TabletPredictor()


def process_tablet(data, reload=0):
    """
    处理平板数据
    :param data: 平板数据
    :param reload: 是否重新加载模型
    :return:
    """
    starttime=time.time()
    if reload == 1:
        tablet_predictor.load_models()
        return {'code': 0, 'message': 'reload success', 'results': []}

    data = process_predict_data(data)
    if data.empty:
        logger.error('data is empty after preprocess')
        return {'code': 1004, 'message': 'no data after preprocess', 'results': []}

    logger.info('predict data@{}'.format(data['sku_id'].tolist()))
    results = tablet_predictor.predict_price(data)
    logger.info('predict done data total elapsed time @{:.6f} s'.format(time.time() - starttime))

    return {'code': 0, 'message': 'success', 'results': results}
